-- MagicMirror
-- Created by cheny3 Sep/18/2016
-- 魔镜

-- 初始化
function initMagicMirror(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    -- 镜子配置
    local mirrorId = self.gridData.mirror_id;
    local iconPath = getLevelItemIconPath(MagicMirrorM.query(mirrorId, "icon"));

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            local dialog = MagicMirrorM.query(mirrorId, "desc");
            local title = MagicMirrorM.query(mirrorId, "title");

            -- 打开建筑界面
            self:openBuildingView(nil, 0, dialog, nil, title, iconPath);

            -- 音效
            AudioM.playFx("workshop_store");
        end
    end

    -- 建筑图标
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
    self.icon:setPositionY(tonumber(buildingInfo["icon_offset_y"]));
end
